package com.example.calendar;

import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import java.util.Calendar;
public class MyCalendar extends LinearLayout {
    public MyCalendar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(LinearLayout.VERTICAL);
        LinearLayout topLayout = new LinearLayout(context);
        LinearLayout bottomLayout = new LinearLayout(context);

        LinearLayout.LayoutParams topLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        topLp.weight=2;
        topLayout.setBackground(getResources().getDrawable(R.drawable.topstyle));
//        topLayout.setBackground(ContextCompat.getDrawable(context, R.drawable.bottomstyle));

        LinearLayout.LayoutParams bottomLP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        bottomLP.weight=5;
        bottomLayout.setBackground(ContextCompat.getDrawable(context, R.drawable.bottomstyle));

        //获取系统的月份和天数
        Calendar date = Calendar.getInstance();
        String strMonth = date.get(Calendar.MONTH)+1+"月";
        String strDay = String.valueOf(date.get(Calendar.DAY_OF_MONTH));
        //放置月份
        TextView tvMonth = new TextView(context);
        tvMonth.setText(strMonth);
        tvMonth.setTextSize(TypedValue.COMPLEX_UNIT_SP,20);
        tvMonth.setTextColor(0xffffffff);
        //放置天数
        TextView tvDay = new TextView(context);
        tvDay.setText(strDay);
        tvDay.setTextSize(TypedValue.COMPLEX_UNIT_SP,30);
        tvDay.setTextColor(0xff000000);
        //文本居中放置
        topLayout.setGravity(Gravity.CENTER);
        bottomLayout.setGravity(Gravity.CENTER);

        topLayout.addView(tvMonth);
        bottomLayout.addView(tvDay);
        this.addView(topLayout,topLp);
        this.addView(bottomLayout,bottomLP);
    }
}
